package com.wdm.sms.commonutil.config.exception;

import com.wdm.sms.commonutil.utils.r.R;
import com.wdm.sms.commonutil.utils.result.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: wudaiming
 * @date: 2023/3/13
 * @description: 自定义运行时异常
 * @version: 1.0
 */
@RestControllerAdvice
public class GlobalExceptionConfig{

    //全局异常处理，执行的方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail().message("执行全局异常处理...");
    }

    //自定义异常处理
    @ExceptionHandler(MyException.class)
    public R<MyException> handle(MyException e){
        e.printStackTrace();
        return R.exception(e.getCode(),e.getMessage());
    }

    //特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("执行特定异常处理...");
    }

    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.fail().code(205).message("没有操作权限");
    }
}